﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Xml;
using ExcelDataReader;
using SdmxMl.SmsImportCfg;
using SdmxMl.Common;
using SdmxMl.Structure;
using SdmxMl.Manager;

namespace SdmxMl.Excel
{
    class valFromTo
    {
        public valFromTo()
        {
            from = to = string.Empty;
        }

        private string from;
        public string From
        {
            get{ return from; }
            set{
                DateTime date;
                if (DateTime.TryParse(value, out date) == false)
                    from = string.Empty;
                else
                    from = date.ToString("yyyy-MM-dd");
            }
        }
        private string to;
        public string To
        {
            get{ return to; }
            set{
                DateTime date;
                if (DateTime.TryParse(value, out date) == false)
                    to = string.Empty;
                else
                    to = date.ToString("yyyy-MM-dd");
            }
        }

    }


    public class ImportHierarchyValidity
    {
        private ExcelDataReader.ExcelDataReader spreadsheet = null;
        private int colValidFrom;
        private int colValidTo;
        private Dictionary<string, valFromTo> dicoValidity;


        private ExcelDataReader.ExcelDataReader LoadSources(string propertyPath)
        {
            ExcelDataReader.ExcelDataReader spreadsheet = null;
            if (propertyPath == null)
                throw new ApplicationException("Please provides path.");

            FileStream fs = null;
            try
            {
                fs = new FileStream(propertyPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                spreadsheet = new ExcelDataReader.ExcelDataReader(fs);
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }

            return spreadsheet;
        }

        public void UpdateHierarchy(HierarchyType hi, string xlPath)
        {
            spreadsheet = LoadSources(xlPath);
            dicoValidity = new Dictionary<string,valFromTo>();

            // Establich dictionary
            DataTable dt = spreadsheet.WorkbookData.Tables[0];
            WorkSheetHelper wh = new WorkSheetHelper(dt);

            colValidFrom = 3;
            colValidTo = 4;
            for (int row = 1; row <= dt.Rows.Count; row++)
            {
                if (row > 1) // 1 header rows
                {

                    string from = wh.GetCellContent(row, colValidFrom);
                    string to = wh.GetCellContent(row, colValidTo);

                    // Filled with date
                    if (from.Length > 5 || to.Length > 5)
                    {
                        valFromTo vft = new valFromTo();
                        vft.From = from;
                        vft.To = to;
                        // Real date(s)
                        if (vft.From.Length > 0 || vft.To.Length > 0)
                        {
                            string key = wh.GetCellContent(row, 1) + "-" + wh.GetCellContent(row, 2);
                            if (dicoValidity.ContainsKey(key) == false)
                                dicoValidity.Add(key, vft);
                            else
                            {
                                throw new ApplicationException("Key: " + key.ToString());
                            }
                        }
                    }

                }
            }

            // Now proceed hierarchy
            if (hi != null)
            {
                RecurseVft(hi.CodeRefList, null);
            }

        }

        private void RecurseVft(CodeRefTypeList crtl, string parentId)
        {
            foreach (CodeRefType crt in crtl)
            {
                    string key = crt.CodeID;
                    if (parentId != null)
                        key = parentId + "-" + crt.CodeID;
                    if (dicoValidity.ContainsKey(key))
                    {
                        crt.ValidFrom = dicoValidity[key].From;
                        crt.ValidTo = dicoValidity[key].To;
                    }

                    RecurseVft(crt.CodeRefList, crt.CodeID);
            }
        }

    }
}
